package com.rgs.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.rgs.ssm.domain.Permission;
import com.rgs.ssm.domain.Role;
import com.rgs.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Ren Gaoshuai
 * @date 2021-05-29 12:52
 *
 * 角色模块 控制层
 **/
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 根据角色id查询角色，并查询出该角色未有的权限
     * @param roleId
     * @return
     */
    @RequestMapping("/findRolesByIdAndAllPermission.do")
    public ModelAndView findRolesByIdAndAllPermission(@RequestParam(name = "id",required = true) Integer roleId){
        ModelAndView mv = new ModelAndView();
        //查询对应的角色
        Role role = roleService.findById(roleId);
        mv.addObject("role",role);

        //查询出该角色未有的权限
        List<Permission> permissionList = roleService.findOtherPermission(roleId);
        mv.addObject("permissionList",permissionList);

        mv.setViewName("role-permission-add");
        return mv;
    }

    /**
     * 处理 添加角色权限的请求，（可添加多个）
     * @param roleId
     * @param ids
     * @return
     */
    @RequestMapping("/addPermissionToRole.do")
    public String addPermissionToRole(@RequestParam(name = "roleId",required = true) Integer roleId,@RequestParam(name = "ids",required = true)Integer[] ids){
        roleService.addPermissionToRole(roleId,ids);
        return "redirect:/role/findAll.do";
    }

    /**
     * 获取所有的角色信息
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required =true,defaultValue = "5")Integer size){
        ModelAndView mv = new ModelAndView();
        List<Role> roles = roleService.findAll(page, size);
        PageInfo pageInfoUser = new PageInfo(roles);
        mv.addObject("roleList",pageInfoUser);
        mv.setViewName("role-list");
        return mv;
    }

    /**
     * 根据角色的id查询对应的角色
     * @param roleId
     * @return
     */
    @RequestMapping("/showDetails.do")
    public ModelAndView showDetails(@RequestParam(name = "id",required = true)Integer roleId){
        ModelAndView mv = new ModelAndView();
        Role role = roleService.findById(roleId);
        mv.addObject("role",role);
        mv.setViewName("role-show");
        return mv;
    }

    /**
     * 处理新增角色请求
     * @param role
     * @return
     */
    @RequestMapping("/save.do")
    public String saveRole(Role role){
        roleService.saveRole(role);
        return "redirect:/role/findAll.do";
    }


    /**
     * 处理删除的请求
     * @return
     */
    @RequestMapping("/deleteById.do")
    public String delete(@RequestParam(name = "id",required = true) Integer roleId){
        roleService.delete(roleId);
        return "redirect:/role/findAll.do";
    }

    /**
     * 去修改的页面
     * @param roleId
     * @return
     */
    @RequestMapping("/toUpdateById.do")
    public ModelAndView toUpdate(@RequestParam(name = "id",required = true) Integer roleId){
        ModelAndView mv = new ModelAndView();
        Role role = roleService.findById(roleId);
        mv.addObject("role",role);
        mv.setViewName("role-update");

        return mv;
    }

    /**
     * 处理修改的请求
     * @return
     */
    @RequestMapping("/updateById.do")
    public String update(){

        return "redirect:/role/findAll.do";
    }
}
